package com.zjy.business.system.manager.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.pagehelper.PageInfo;
import com.zjy.business.system.domain.entity.SysUser;
import com.zjy.business.system.domain.manager.form.ManagerSysUserSaveOrUpdateForm;
import com.zjy.business.system.domain.manager.query.ManagerListPageSysUserQuery;
import com.zjy.business.system.domain.manager.vo.ManagerListPageSysUserVo;
import com.zjy.business.system.manager.service.ManagerSysUserService;
import com.zjy.log.annotation.OperationLog;
import com.zjy.log.enums.LogBusinessType;
import com.zjy.web.domain.model.Result;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理-用户管理
 */
@RestController
@RequestMapping("/manager/sysUser")
@RequiredArgsConstructor
@Validated
public class ManagerSysUserController {

    private static final String MODULE_NAME = "后台用户管理";
    final private ManagerSysUserService managerSysUserService;

    /**
     * 新增或修改用户
     */
    @OperationLog(title = MODULE_NAME, logBusinessType = LogBusinessType.INSERT_UPDATE)
    @PostMapping("/saveOrUpdate")
    @SaCheckPermission("system:sysUser:save_update")
    public Result<Void> saveOrUpdate(@RequestBody ManagerSysUserSaveOrUpdateForm form) {
        managerSysUserService.saveOrUpdate(form);
        return Result.SUCCESS();
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return
     */
    @OperationLog(title = MODULE_NAME, logBusinessType = LogBusinessType.DELETE)
    @GetMapping("/remove")
    @SaCheckPermission("system:sysUser:remove")
    public Result<Void> remove(Long id) {
        managerSysUserService.remove(id);
        return Result.SUCCESS();
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/listPageSysUser")
    @SaCheckPermission("system:sysUser:pageList")
    public Result<PageInfo<ManagerListPageSysUserVo>> listPageSysUser(ManagerListPageSysUserQuery query) {
        return Result.SUCCESS(managerSysUserService.listPageSysUser(query));
    }

    /**
     * 重置密码
     * @param userId 用户id
     */
    @GetMapping("/resetPassword")
    @SaCheckPermission("system:sysUser:save_update")
    @OperationLog(title = MODULE_NAME, logBusinessType = LogBusinessType.UPDATE)
    public Result<Void> resetPassword(@NotNull(message = "用户id不能为空") Long userId) {
        managerSysUserService.resetPassword(userId);
        return Result.SUCCESS();
    }

    /**
     * 用户下拉列表接口
     */
    @GetMapping("/userDownList")
    public Result<List<SysUser>> userDownList() {
        return Result.SUCCESS(managerSysUserService.userDownList());
    }

}
